﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using System.Linq.Dynamic.Core;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Mvc;
using Wz.Interface;
using Wz.Entities;
using Wz.Dto;

namespace Wz.Services
{
    public class ProApplyService : ApplicationService, IProApplyService
    {
        //依赖注入
        private readonly IRepository<ProApply, Guid>  _proApplies;
        public ProApplyService(IRepository<ProApply, Guid> _proAppliess)
        {
            _proApplies = _proAppliess;
        }
        /// <summary>
        /// 申领显示
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// 
        [HttpGet]
        public async Task<PagedResultDto<ProApplyDto>> ProApplyShow(PagedAndSortedResultRequestDto input)
        {
            var query = _proApplies.OrderBy(input.Sorting);
            var count = await query.CountAsync();
            List<ProApply> proApplies = await _proApplies.ToListAsync();
            List<ProApplyDto> proApplyDtos = ObjectMapper.Map<List<ProApply>, List<ProApplyDto>>(proApplies);
            return new PagedResultDto<ProApplyDto>(count, proApplyDtos);
        }
        /// <summary>
        /// 申领添加
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public async Task<ProApplyDto> ProApplyAdd([FromForm]AddProApplyDto obj)
        {
            ProApply pro=new ProApply(
                    GuidGenerator.Create(),
                    DateTime.Now,
                    obj.Title,
                    obj.Info,
                    obj.Receiver,
                    obj.ClaimTime,
                    obj.Remarks,
                    obj.States,
                    obj.StateName
                );
            var result = await _proApplies.InsertAsync(pro);

            return ObjectMapper.Map<ProApply, ProApplyDto>(result);
        }
        /// <summary>
        /// 申领删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// 
        [HttpDelete]
        public async Task ProApplyDel(Guid id)
        {
            await _proApplies.DeleteAsync(id);
        }
        /// <summary>
        /// 申领修改
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        /// 
        [HttpPut]
        public async Task<ProApplyDto> ProApplyUpt([FromForm]UptProApplyDto obj)
        {
            ProApply pro = new ProApply(
                    obj.ID,
                    DateTime.Now,
                    obj.Title,
                    obj.Info,
                    obj.Receiver,
                    obj.ClaimTime,
                    obj.Remarks,
                    obj.States,
                    obj.StateName
                );
            var result = await _proApplies.UpdateAsync(pro);

            return ObjectMapper.Map<ProApply, ProApplyDto>(result);
        }
        /// <summary>
        /// 申领回显
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// 
        [HttpGet]
        public async Task<ProApplyDto> ProApplyFind(Guid id)
        {
            var result = await _proApplies.FindAsync(id);
            return ObjectMapper.Map<ProApply,ProApplyDto>(result);
        }

        
    }
}
